/**
 * @author klv
 */
var UsageTrack = function(){
    this.Path = String.format("{0}{1}",version.rtq , "userTack.jsp");
    this.Oper = {
        New: "New",
        Add: "Add",
        Delete: "Del",
        Rename: "Rename",
        Save: "Save",
        Saveas: "SaveAs",
        Open: "Open",
		Close: "Close"
    };
    this.Source = {
        Workspace: "Workspace",
        Alert: "Alert",
        Tape: "Tape",
        Watchlist: "Watchlist",
        Chart: "Chart",
        News: "News",
        Quicktake: "Quicktake",
        MarketDept: "MarketDept",
        TimeAndsales: "Time&sales",
        Options: "Options",
        Research: "Research",
        Quote: "Quote",
        Forex: "Forex",
        Calendars: "Calendars",
        Commodity: "Commodity",
        Market: "Market",
		Login:"Login",
		LogOut:"Logout"
    };
    this.Group = {
        Basic: "Basic",
        Activity: "Activity",
        DataUsage: "Datausage",
        Error: "Error"
    };
    this._init();
};
UsageTrack.prototype = {
    InsertAnRecord: function(obj){
        this._insertAnRecord(obj);
    },
	InsertErrorRecord:function(source,oper,name,id,comment){
		this.OperateComponent(this.Group.Error,source,
		oper,name,id,comment
		);
	},
	InsertCustomRecord:function(group,souce,detail){
		var obj = this._creatOperationObject();
        obj.Group = group;
        obj.Source = source;
        $.extend(obj.Detail,detail);
		this.InsertAnRecord(obj);
	},
    OperateComponent: function(group,source,oper,name,id,comment){
        this.OperateTicker(group,source,oper,name,id,comment,"");
    },
    OperateTicker: function(group,source,oper,name,id,comment,ticker){
        var obj = this._creatOperationObject();
        obj.Group = group;
        obj.Source = source;
        obj.Detail.Oper=oper;
		obj.Detail.Name=name||"";
        obj.Detail.Ticker=ticker||"";
		obj.Detail.ID=id;
		obj.Detail.Comment=comment;
        obj.Detail.Time = this._getDate();
        this.InsertAnRecord(obj);
    },
    Serialize: function(){
        var self = this;
        var temp = [];
        $.extend(true,temp,self.Record);
        self._init();
        if (temp.length > 0) {
            var str = $.toJSON(temp);
            DBHelper.post(self.Path, {
                detail: str
            },function(d){},5000);
        }
    },
	_creatOperationObject:function(){
		var obj={Group:"",Source:"",Detail:""};
		obj.Detail=this._createDetailObject();
		return obj;
	},
	_createDetailObject:function(){
		return {"Oper":"","Name":"","ID":"",Ticker:"","Time":"",Comment:""};
	},
    _init: function(){
        this.Record = [];
    },
    _getDate: function(){
        var d = new Date();
        var s = "";
        s += (d.getUTCMonth() + 1) + "/";
        s += d.getUTCDate() + "/";
        s += d.getUTCFullYear() + " ";
        s += d.getUTCHours() + ":";
        s += d.getUTCMinutes() + ":";
        s += d.getUTCSeconds();
        return s;
    },
    _insertAnRecord: function(obj){
        this.Record.push(obj)
    }
};
var Qs_UserTrace = new UsageTrack();
